<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>报关报检业务管理</title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript" src="../js/ajaxutil.js"></script>
</head>
<script type="text/javascript">
    var bstate = "${param.declaraState }";
    var comId = "${sessionScope.CURRENT_USER.company.id}";
    var formSetting = {
        title: "查询报关报检业务信息",
        module: 'query',
        columns: [{
            name: "businessId",
            label: "业务编码"
        },
            {
                name: "state",
                label: "业务状态",
                type: "dropdown",
                dropdownType: "ajax",
                url: "../base/dropdown-declarationState",
                op: "eq",
                minLength: 0
            },
            {
                name: "so",
                label: "SO号"
            }, {
                name: "creator",
                label: "下单操作人"
            }, {
                name: "container_no",
                label: "集装箱号"
            }, {
                name: "contract_no",
                label: "合同协议号"
            }, {
                name: "company_name",
                label: "经营单位"
            }, {
                name: "confirmTime",
                label: "业务提交时间从",
                type: "date",
                op: "ge"
            }, {
                name: "confirmTime",
                label: "到",
                type: "date",
                op: "le"
            }],
        buttons: {
            custom: [
                {
                    id: "updatebtn0",
                    value: "查看详细",
                    click: function () {
// 						var id = $("#declarationsDataGrid").jqGrid("getGridParam",
// 						"selrow");
// 						if(id) {
// // 							window.location.href="declaration-bizView?id="+id;
// // 							window.location.href="business-checkDetail?id="+id+"&singleType=D&checkDetail=T&identification=list";

// 						} else {
// 							jw.alert("请点击需要操作的数据!");
// 						}
                        var selectedRowId = $("#declarationsDataGrid").getGridParam("selrow");
                        if (selectedRowId) {
                            var selectedRow = $("#declarationsDataGrid").getRowData(selectedRowId);
                            ajaxLoader.ajaxPost(
                                    '../datalock/onUnlock?opaId=VDBS&bidId='
                                    + selectedRow.id, {},
                                    function (data) {
                                        if (!data) {
                                            jw.alert(data.errmsg);
                                        }
                                        if ("200" == data.errcode) {
                                            if (selectedRow.state == "草稿状态") {
                                                //selectedRow.busiId
                                                location.href =
                                                        "business-updateSingle?id=" + selectedRow.id
                                                        + "&singleType=D";
                                            } else {
                                                location.href =
                                                        "business-updateSingle?id=" + selectedRow.id
                                                        + "&singleType=D&identification=list";
                                            }
                                        }
                                        else {
                                            jw.alert(data.errmsg);
                                        }
                                    });
                        } else {
                            jw.alert("请点击需要操作的数据!");
                        }
                    }
                },
                {
                    id: "checkRejectContent",
                    value: "查看驳回",
                    click: function () {
                        var id = $("#declarationsDataGrid").jqGrid("getGridParam",
                                                                   "selrow");
                        var rowDatas = $("#declarationsDataGrid").jqGrid('getRowData', id);
                        var row = rowDatas["state"];
                        if (row == "驳回状态" || row == "草稿状态") {
                            //window.location.href="rejectContent-rejectList?id="+id;
                            jw.dialog({
                                          iframe: '../business/rejectContent-rejectList?id=' + id,
                                          title: "查看驳回",
                                          iframeFetchTitle: false,
                                          width: 800,
                                          height: 300
                                      });
                        } else {
                            jw.alert("请选择驳回状态业务的数据!");
                        }
                    }
                },
                {
                    id: "updatebtn00",
                    value: "修改业务",
                    click: function () {
                        var id = $("#declarationsDataGrid").jqGrid("getGridParam",
                                                                   "selrow");
                        if (id) {
                            var ret = $("#declarationsDataGrid").jqGrid('getRowData', id);
                            var data = "id=" + ret.id;
                            $.ajax({
                                       url: "declaration-checkBizModify",
                                       cache: false,
                                       dataType: "json",
                                       data: data,
                                       success: function (data) {
                                           if (!data) {
                                               return;
                                           }
                                           var msg = data.msg ? $.parseJSON(data.msg) : "";
                                           if (msg.state == 1) {
                                               jw.confirm("确定要修改该业务?", function () {
//                                                              jw.alert($.parseJSON(data.msg).msg);
                                                              window.location.href =
                                                                      "business-updateSingle?id=" + id
                                                                      + "&singleType=D";
                                                              //window.location.href="declaration-bizModify?id="+ret.id;
                                                          },
                                                          function () {
                                                              return;
                                                          });
                                           }
                                           else if (msg !== undefined && msg != "" && msg.state
                                                                                      == 0) {
                                               jw.alert("只有处于草稿和驳回状态的业务才可以修改。");
                                           }
                                           else {
                                               jw.alert(msg.msg);
                                           }
                                       }
                                   });
                        } else {
                            jw.alert("请点击需要操作的数据!");
                        }
                    }
                },
                {
                    id: "delbtn",
                    value: "删除业务",
                    click: function () {
                        var selectedIds = $("#declarationsDataGrid").jqGrid("getGridParam",
                                                                            "selrow");
                        var rowDatas = $("#declarationsDataGrid").jqGrid('getRowData', selectedIds);
                        var row = rowDatas["state"];
                        if (row == "草稿状态") {
                            if (confirm("确定要删除选择的项?")) {
                                var data = "id=" + selectedIds;
                                $.ajax({
                                           url: "declaration-del",
                                           dataType: "json",
                                           data: data,
                                           cache: false,
                                           success: function (data) {
                                               var msg = $.parseJSON(data.msg);
                                               if (msg.state == "1") {
                                                   $("#declarationsDataGrid").jqGrid("setGridParam",
                                                                                     {
                                                                                         search: true
                                                                                         // 将jqGrid的search选项设为true
                                                                                     }).trigger(
                                                           "reloadGrid", [{
                                                               page: 1
                                                           }]);
                                                   jw.alert(msg.msg);
                                               } else {
                                                   jw.alert(msg.msg);

                                               }
                                           }
                                       });
                            }
                        } else {
                            jw.alert("请选择草稿状态业务的数据!");
                        }
                    }
                },
                {
                    id: "updatebtn1",
                    value: "提交业务",
                    click: function () {
                        var id = $("#declarationsDataGrid").jqGrid("getGridParam",
                                                                   "selrow");
                        if (id) {
                            //var ret = $("#declarationsDataGrid").jqGrid('getRowData',id);
                            //var iddata = "id=" + ret.id;
                            //$.ajax({
                            //	url : "declaration-checkBizConfirm",
                            //	cache: false,
                            //	dataType : "json",
                            //	data : iddata,
                            //	success : function(data) {
                            //		if($.parseJSON(data.msg).state==1) {
                            //TODO: 添加弹窗询问支付
                            if ($("#declarationsDataGrid").jqGrid("getGridParam",
                                                                  "selarrrow").length <= 5) {
                                jw.confirm("是否立即提交", function () {
                                    var id = $("#declarationsDataGrid").jqGrid("getGridParam",
                                                                               "selarrrow");
                                    var params = (id + "").replace(/,/gi, '&declaration_ids=');
                                    window.location.href =
                                            "declaration-confirmPay?declaration_id=" + params;
                                });
                            }
                            else {
                                jw.alert("批量提交功能目前暂时只能一次提交不超过5票单。")
                            }
                            //		}
                            //		else if($.parseJSON(data.msg).state==0) {
                            //			jw.alert("只有处于草稿和驳回状态的业务才可以提交。");
                            //		}
                            //		else {
                            //			jw.alert($.parseJSON(data.msg).msg);
                            //		}
                            //	}
                            //});
                        } else {
                            jw.alert("请点击需要操作的数据!");
                        }
                    }
                },
                {
                    id: "exportExcel",
                    value: "导出数据",
                    click: function () {
                        var businessId = $("#form_in_control_1").val();
                        var state = $("#form_in_control_2").val();
                        var so = $("#form_in_control_3").val();
                        var creator = $("#form_in_control_4").val();
                        var container_no = $("#form_in_control_5").val();
                        var contract_no = $("#form_in_control_6").val();
                        var company_name = $("#form_in_control_7").val();
                        var confirm_time_start = $("#form_in_control_8").val();
                        if (confirm_time_start != "") {
                            confirm_time_start = confirm_time_start + " 00:00:00";
                        }
                        var confirm_time_end = $("#form_in_control_9").val();
                        if (confirm_time_end != "") {
                            confirm_time_end = confirm_time_end + " 23:59:59";
                        }
                        var companyId = $("#companyId").val();
// 						var url = "http://localhost:8075/WebReport/ReportServer?reportlet=declarationList.cpt&op=write&companyId="
                        var url = "http://219.130.222.62:7080/report/ReportServer?reportlet=gtimp/declarationList.cpt&op=write&companyId="
                                  + companyId
                                  + "&businessId="
                                  + businessId
                                  + "&state="
                                  + state
                                  + "&so="
                                  + so
                                  + "&creator="
                                  + creator
                                  + "&container_no="
                                  + container_no
                                  + "&contract_no="
                                  + contract_no
                                  + "&company_name="
                                  + company_name
                                  + "&confirm_time_start="
                                  + confirm_time_start
                                  + "&confirm_time_end="
                                  + confirm_time_end;//本机
                        window.open(url);
                    }
                }]
        },
        grid: {
            id: "declarationsDataGrid",
            title: "查询结果",
            url: "declaration-findList",
            multiselect: true,
            //multiselect : true,
            /* postData : {
             "pid" : pid
             }, */
            postData: {
                "declaraState": bstate
            },
            colModel: [{
                index: "ID",
                name: "id",
                hidden: true
            }, {
                index: "businessId",
                name: "businessId",
                label: "业务编号"
            }, {
                index: "container_no",
                name: "container_no",
                label: "集装箱号"
            }, {
                index: "contract_no",
                name: "contract_no",
                label: "合同协议号"
            }, {
                index: "company_name",
                name: "company_name",
                label: "经营单位"
            }, {
                index: "supNAME",
                name: "supCompany.name",
                label: "业务供应商"
            }, {
                index: "shipper",
                name: "shipper.company",
                label: "实际托运人",
                hidden: true
            }, {
                index: "confirmTime",
                name: "confirmTime",
                label: "业务提交时间",
                formatter: 'date',
                formatoptions: {
                    srcformat: 'Y-m-d H:i:s',
                    newformat: 'Y-m-d H:i:s'
                }
            }, {
                index: "validateState",
                name: "state",
                label: "业务状态"
            }, {
                index: "operateState",
                name: "operateState",
                label: "业务操作进度",
                hidden: true
            }, {
                index: "informState",
                name: "declarationInform.confirmState",
                label: "资料寄送提醒进度",
                hidden: true
            }, {
                name: "creator",
                label: "下单操作人"
            }, {
                name: "validator.name",
                label: "审核人"
            }, {
                name: "validator.mobile",
                label: "审核人电话"
            }, {
                name: "download",
                label: "底单下载"
            }, {
                name: "bpk",
                label: "bpk",
                hidden: true
            }, {
                index: "sno",
                name: "sno",
                label: "报关单号"
            }, {
                index: "uploadDiDan",
                name: "uploadDiDan.id",
                label: "uploadDiDan",
                hidden: true
            }],
            sortname: "businessId",
            sortorder: "desc",
            jsonReader: {
//                root: "declarations"
            },
            loadComplete: function (data) {
                var ids = $("#declarationsDataGrid").getDataIDs();
                for (var i = 0; i < ids.length; i++) {
                    var arrayData = $("#declarationsDataGrid").getRowData(
                            ids[i]);
                    if (arrayData.state == "海关放行") {
                        // 						$("#declarationsDataGrid").setCell(ids[i], "operateState", "<span style='cursor:pointer;' onclick=\"findBizState(\'" + i + "\', 'state', \'"+arrayData.id+"\');\"><font color='red'>点击查看</font><span>");
                        $("#declarationsDataGrid")
                                .setCell(
                                        ids[i],
                                        "state",
                                        "海关放行 <span style='cursor:pointer;' onclick=\"findBizState(\'"
                                        + i
                                        + "\', 'state', \'"
                                        + arrayData.id
                                        + "\');\"><font color='red'>进度查询</font><span>");
                    } else if (arrayData.state == "商检放行") {
                        $("#declarationsDataGrid")
                                .setCell(
                                        ids[i],
                                        "state",
                                        "商检放行 <span style='cursor:pointer;' onclick=\"findBizState(\'"
                                        + i
                                        + "\', 'state', \'"
                                        + arrayData.id
                                        + "\');\"><font color='red'>进度查询</font><span>");
                    } else if (arrayData.state == "已完成") {
                        $("#declarationsDataGrid")
                                .setCell(
                                        ids[i],
                                        "state",
                                        "已完成 <span style='cursor:pointer;' onclick=\"findBizState(\'"
                                        + i
                                        + "\', 'state', \'"
                                        + arrayData.id
                                        + "\');\"><font color='red'>进度查询</font><span>");
                        //$("#declarationsDataGrid").setCell(ids[i], "operateState", "业务已完成");
                        $($("#" + arrayData.id).find("td")[6]).addClass(
                                "biz_state_finish");
                    } else {
                        $("#declarationsDataGrid").setCell(ids[i],
                                                           "operateState", "业务未安排");
                    }
                    if ($(arrayData).attr('declarationInform.confirmState') == "0") {
                        $("#declarationsDataGrid").setCell(ids[i],
                                                           "declarationInform.confirmState",
                                                           "已发送，待确认");
                    } else if ($(arrayData).attr(
                                    'declarationInform.confirmState') == "1") {
                        $("#declarationsDataGrid").setCell(ids[i],
                                                           "declarationInform.confirmState",
                                                           "已确认接收");
                        $($("#" + arrayData.id).find("td")[7]).addClass(
                                "biz_state_finish");
                    } else {
                        $("#declarationsDataGrid").setCell(ids[i],
                                                           "declarationInform.confirmState", "未发送");
                    }

                    //处理合同协议号默认值
                    if ($(arrayData).attr('contract_no') == "最长20个字符") {
                        $("#declarationsDataGrid").setCell(ids[i],
                                                           "contract_no", " ");
                    }
                    //底单下载链接
                    var uploadDiDanId = $(arrayData).attr('uploadDiDan.id');
                    $("#declarationsDataGrid")
                            .setCell(
                                    ids[i],
                                    "download",
                                    "<div align='center'><a style='cursor:pointer;' id='download_img_"
                                    + i + "' href='javascript:void(0)' onclick=\"dowmload(\'"
                                    + i
                                    + "\', 'download', \'"
                                    + arrayData.id
                                    + "\', \'"
                                    + uploadDiDanId
                                    + "\');\"><font color='red'>下载</font></a></div>");
                }
            },
            ondblClickRow: function () {
                var selectedRowId = $("#declarationsDataGrid").getGridParam(
                        "selrow");
                if (selectedRowId) {
                    var selectedRow = $("#declarationsDataGrid").getRowData(
                            selectedRowId);
                    if (selectedRow.state == "草稿状态") {
                        //selectedRow.busiId
                        location.href = "business-updateSingle?id="
                                        + selectedRow.id + "&singleType=D";
                    } else {
                        location.href = "business-up dateSingle?id="
                                        + selectedRow.id
                                        + "&singleType=D&identification=list";
                    }
                }
            }
        }
    };
    $(document).ready(function () {
        $("#declarationsDataTable").mrdForm(formSetting);
        //TODO:
        if (bstate == "DS00") {
            $("#checkRejectContent").hide();
        } else if (bstate == "DS01") {
            $("#checkRejectContent").hide();
            $("#updatebtn00").hide();
            $("#delbtn").hide();
            $("#updatebtn1").hide();
        }
    });

    function setGridCell(i, name, value) {
        var ids = $("#declarationsDataGrid").getDataIDs();
        $("#declarationsDataGrid").setCell(ids[i], name, value);
    }

    function findBizResult(bizId) {
        jw.dialog({
                      iframe: 'declaration-findBizResult?id=' + bizId
                  });
    }

    function findBizState(i, colName, bizId) {
        var data = "id=" + bizId;
        $.ajax({
                   url: "declaration-findBizState",
                   cache: false,
                   dataType: "json",
                   data: data,
                   success: function (data) {
                       if (data.msg == " 三联单号：未能找到相关数据") {
                           jw.dialog({
                                         iframe: 'declaration-findBizPassInfo?id=' + bizId
                                     });
                       } else {
                           findBizResult(bizId);
                       }
//  				setGridCell(i, colName, data.msg);
                   }
               });
//  		setGridCell(i, colName, "loadding...");
    }

    function dowmload(i, colName, bizId, uploadDiDanId) {
        var data = "id=" + bizId + "&uploadDiDanId=" + uploadDiDanId;
        $.ajax({
                   url: "declaration-dowmlaodDeclara",
                   cache: false,
                   dataType: "json",
                   data: data,
                   success: function (data) {
                       if (!data) {
                           return;
                       }
                       var msg = data.msg ? $.parseJSON(data.msg) : "";
                       if (msg !== undefined && msg != "" && msg.state == 0) {
                           alert(msg.msg);
                       } else if (msg.state == 2) {
                           window.location.href =
                                   "business/download?attachment.id=" + uploadDiDanId;
                       } else {
// 					$("#download_img_" + i).attr("href", "declaration-downloadDeclarationFile?id="+bizId);
                           window.location.href = "declaration-downloadDeclarationFile?id=" + bizId;
                       }
                   }
               });
    }
</script>
<body>
<div id="declarationsDataTable"></div>
<input id="companyId" type="hidden" value="${companyId }"/>
<!-- 	<a id="download_img" href="javascript:void(0)"></a> -->
</body>
</html>
